package Quartz;

import ibatis.IbatisHelper;

import java.util.*;
import java.util.Date;
import java.sql.*;

import Eclass.Exam.ExamModel;
import Eclass.Question.QuestionModel;

import com.ibatis.sqlmap.client.SqlMapClient;

public class QuartzManager {
	private SqlMapClient ibatis;

	public QuartzManager() {
		this.ibatis = IbatisHelper.getSqlMapInstance();
	}

//	public QuartzModel selectExam() {
//		QuartzModel bean = new QuartzModel();
//		try {
//			bean = (QuartzModel) ibatis.queryForObject("quartz.selectExam", "");
//
//			System.out.println("\n\nPanjang List : ");
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}
//		return bean;
//	}

	
	
	public List selectExam(String isOpen) {
		List result = null;
		
		try {
			Map map = new HashMap();
		if(isOpen.equals("0")){
			map.put("isOpen", isOpen);
			map.put("start", "1");
		}else{
			map.put("isOpen", isOpen);
//			map.put("start", start);
		}
			
			result = ibatis.queryForList("quartz.selectExam", map);
			
			System.out.println("\n\nPanjang List : " + result.size());
//		
//			for(int i=0;i<result.size();i++){
//				result
//			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public QuartzModel selectExamFor(String examPrepId) {
		QuartzModel bean=new QuartzModel();
		
		try {
			bean = (QuartzModel) ibatis.queryForObject("quartz.selectExamFor", examPrepId);
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return bean;
	}
	
	
	
	
	
	
	public List selectExamCandidate() {
		List result = null;
		
		try {
			result = ibatis.queryForList("quartz.selectExamCandidate","");
			
			System.out.println("\n\nPanjang List : " + result.size());

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public List reloadExamCandidate() {
		List result = null;
		
		try {
			result = ibatis.queryForList("quartz.reloadExamCandidate","");
			
			System.out.println("\n\nPanjang List : " + result.size());

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	
	public List selectUserScore(String examPrepId) {
		List result = null;
		try {
			result = ibatis.queryForList("quartz.selectUserScore", examPrepId);
			
			System.out.println("\n\nPanjang List : " + result.size());

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public List selectUserScoreCandidate(String examPrepId) {
		List result = null;
		try {
			result = ibatis.queryForList("quartz.selectUserScoreCandidate", examPrepId);
			
			System.out.println("\n\nPanjang List : " + result.size());

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	
	public boolean updateOpen(String examId,String isOpen) {
		
		try {
			Map map = new HashMap();
			map.put("examId", examId);
			map.put("isOpen", isOpen);
			ibatis.startTransaction();
				this.ibatis.update("quartz.updateOpen", map);
			ibatis.commitTransaction();
		} catch (SQLException sqe) {
			// TODO: handle exception
			sqe.printStackTrace();
		} finally {
			try {
				ibatis.endTransaction();
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		return true;
	}
	
	public boolean countGrade(Integer batchClassId) {
		
		try {
			ibatis.startTransaction();
				this.ibatis.update("quartz.calculateGrade", batchClassId);
			ibatis.commitTransaction();
		} catch (SQLException sqe) {
			// TODO: handle exception
			sqe.printStackTrace();
		} finally {
			try {
				ibatis.endTransaction();
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		return true;
	}
}
